package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.modular.system.entity.BtyArchives;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWx;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyWxMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BtyWxService extends ServiceImpl<BtyWxMapper, BtyWx> {

    @Resource
    private BtyWxMapper mapper;

    /**
     * 获取档案管理列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:05 PM
     */

    public Page<Map<String, Object>> list(String name, String shopName, String tel, String phone,
                                          String post, Long deptId, String regionOne, String regionTwo) {
        Page page = LayuiPageFactory.defaultPage();

        return this.baseMapper.list(page, name, shopName, tel, phone, post, deptId, regionOne, regionTwo);
    }

    /**
     * 同步钉钉手机号
     *
     * @param oldTels,mobile
     * @return
     */
    public void updateBtyWxPhone(String oldTels,String mobile){
        //管理员表中查找是否有符合的手机号
        List<BtyWx> btyWxList=new LambdaQueryChainWrapper<>(this.mapper)
                .eq(BtyWx::getPhone,oldTels)
                .list();
        //根据已查到的手机号匹配管理员表手机号 匹配到先更新保存原手机号，然后替换新手机号
        btyWxList.forEach(i -> {
            if(!i.getPhone().equals(mobile)){
                LambdaQueryWrapper<BtyWx> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(BtyWx::getPhone,mobile);
                BtyWx btyWxOne = this.baseMapper.selectOne(queryWrapper);
                if(btyWxOne==null){
                    i.setOldTels(i.getPhone());
                    i.setPhone(mobile);
                    //引入管理员表数据操作层
                    this.mapper.updateById(i);
                }
            }
        });
    }

    /**
     * 添加数据
     *
     * @param btyWx
     * @return
     */
    public int add(BtyWx btyWx) {
        return mapper.add(btyWx);
    }

    /**
     * 查询职务
     *
     * @return
     */
    public List<BtyArchives> getPost() {
        return mapper.getPost();
    }

    /**
     * 查询一级区域
     *
     * @return
     */
    public List<BtyArchives> getRegionOne() {
        return mapper.getRegionOne();
    }

    /**
     * 查询二级区域
     *
     * @param region
     * @return
     */
    public List<BtyArchives> getRegionTwo(String region) {
        return mapper.getRegionTwo(region);
    }

    /**
     * 查询三级区域（店面）
     *
     * @param region
     * @return
     */
    public List<BtyArchives> getRegionThree(String region) {
        return mapper.getRegionThree(region);
    }

    /**
     * 查询数据库中是否存在此人
     *
     * @param openId
     * @return
     */
    public User selectUser(String openId) {
        return mapper.selectUser(openId);
    }

    public List<BtyWx> getWx(String phone) {
        return mapper.getWx(phone);
    }

    public List<BtyWx> ddOpenId() {
        return mapper.ddOpenId();
    }

    /**
     * 查询档案中有openid的人
     *
     * @return
     */
    public List<BtyArchives> allOpenId() {
        return mapper.allOpenId();
    }
    public Integer ddListByName(String name){
        return mapper.ddListByName(name);
    }

    public List<BtyWx> getUserOpenid() {
        return this.baseMapper.getUserOpenid();
    }
}
